package com.java.zhuiyun.api.playerLoginLogs.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.java.zhuiyun.api.playerinformation.entity.PlayerInformation;
import com.java.zhuiyun.api.playerinformation.mapper.PlayerInformationMapper;
import com.java.zhuiyun.api.playerLoginLogs.entity.PlayerLoginLogs;
import com.java.zhuiyun.api.playerLoginLogs.mapper.PlayerLoginLogsMapper;
import com.java.zhuiyun.api.playerLoginLogs.model.PlayerLoginLogsActivityLevelModel;
import com.java.zhuiyun.api.playerLoginLogs.model.PlayerLoginLogsModel;
import com.java.zhuiyun.api.playerLoginLogs.service.PlayerLoginLogsService;
import com.java.zhuiyun.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName PlayerLoginLogsServiceImpl
 * @Description TODO 登录数据统计
 * @Author WX
 * @Date 2023/9/19 8:47
 **/
@Service
public class PlayerLoginLogsServiceImpl implements PlayerLoginLogsService {

    @Autowired
    private PlayerLoginLogsMapper playerLoginLogsMapper;

    @Autowired
    private PlayerInformationMapper playerInformationMapper;

    /*
     * @Author WX
     * @Description 每天凌晨0.01统计前日活跃数据
     * @Date 2023/9/19 8:41
     * @Return
     * @param null
     */
    @Override
    public int insertPlayerLoginLogs() {
        // 查询当日登录人数
        List<PlayerInformation> list = playerInformationMapper.selectPlayerInformationToday();
        PlayerLoginLogs playerLoginLogs = new PlayerLoginLogs();
        // 将当前日期减去一天
        LocalDate previousDate = LocalDate.now().minusDays(1);
        // 转换为 Date 对象
        Date previousDay = Date.from(previousDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        // 设置前一天的时间
        playerLoginLogs.setLoginTime(previousDay);
        // 判断查询到的当日登录人数有没有
        if(list.size() == 0){
            playerLoginLogs.setPlayerNumber(0);
        } else {
            playerLoginLogs.setPlayerNumber(list.size());
        }
        int a = playerLoginLogsMapper.insertPlayerLoginLogs(playerLoginLogs);
        return list.size();
    }

    /*
     * @Author WX
     * @Description 查询活跃量
     * @Date 2023/9/19 14:37
     * @Return
     * @param null
     */
    @Override
    public CommonResult selectPlayerLoginLogs() {
        List<String> qwe = new ArrayList<>();
        List<Integer> qqq = new ArrayList<>();
        LocalDate currentDate = LocalDate.now();
        int daysInMonth = YearMonth.of(currentDate.getYear(), currentDate.getMonth()).lengthOfMonth();
        int dayOfMonth = currentDate.getDayOfMonth();
        PlayerLoginLogsModel playerLoginLogsModel = playerInformationMapper.selectDatePeople();

        for (int i = 1; i <= daysInMonth; i++) {
            PlayerLoginLogsModel playerLoginLogs = playerLoginLogsMapper.selectPlayerLoginLogs(String.valueOf(i));
            Integer name = (playerLoginLogs != null && playerLoginLogs.getDay() != null) ? playerLoginLogs.getDay() : i;
            Integer eng = (playerLoginLogs != null && playerLoginLogs.getPlayerNumber() != null) ? playerLoginLogs.getPlayerNumber() : 0;

            qwe.add(name + "号");
            qqq.add(eng);

            if (i == dayOfMonth) {
                eng = (playerLoginLogsModel != null && playerLoginLogsModel.getPrice() != null) ? playerLoginLogsModel.getPrice() : 0;
                qqq.set(i - 1, eng);
            }
        }

        return CommonResult.ok(CollUtil.newArrayList(qwe, qqq));
    }

    /*
     * @Author WX
     * @Description 查询日周月活量
     * @Date 2023/9/20 9:23
     * @Return
     * @param null
     */
    @Override
    public CommonResult getPlayerLoginLogs() {
        // 查询当前日的数据
        PlayerLoginLogsModel playerLoginLogsModel = playerInformationMapper.selectDatePeople();
        // 查询当前周的数据
        PlayerLoginLogsModel playerLoginLogsModel1 = playerLoginLogsMapper.getPlayerLoginLogsByWeek();
        // 查询当前月的数据
        PlayerLoginLogsModel playerLoginLogsModel2 = playerLoginLogsMapper.getPlayerLoginLogsByMonth();
        PlayerLoginLogsActivityLevelModel playerLoginLogsActivityLevelModel = new PlayerLoginLogsActivityLevelModel();

        // 判断查询到的数据是否为null，如果为null则设为0，否则正常赋值
        int dayActivityLevel = (playerLoginLogsModel != null  && playerLoginLogsModel.getPrice() != null) ? playerLoginLogsModel.getPrice() : 0;
        int weeklyActivity = ( playerLoginLogsModel1 != null && playerLoginLogsModel1.getPrice() != null) ? dayActivityLevel + playerLoginLogsModel1.getPrice(): dayActivityLevel;
        int monthlyActivity = ( playerLoginLogsModel2 != null && playerLoginLogsModel2.getPrice() != null) ? dayActivityLevel + playerLoginLogsModel2.getPrice() : dayActivityLevel;

        // 设置日活量数据
        playerLoginLogsActivityLevelModel.setDayActivityLevel(dayActivityLevel);
        // 设置周活量数据
        playerLoginLogsActivityLevelModel.setWeeklyActivity(weeklyActivity);
        // 设置月活量数据
        playerLoginLogsActivityLevelModel.setMonthlyActivity(monthlyActivity);

        return CommonResult.ok(playerLoginLogsActivityLevelModel);
    }
}
